#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QLabel>
#include <QWidget>
#include "../entity.h"
#include <vector>
#include "clickableLabel.h"
#include "../world.h"
#include "../unit.h"

#include <QPoint>
#include <QTimer>
#include <QString>

#include <QTcpServer>
#include <QTcpSocket>
#include <QMessageBox>
#include <QThread>
#include <iostream>
#include <QMouseEvent>
#include <QLabel>
#include <QTextEdit>


class MovableLabel;

class ConnectThread : public QThread {

    QString hostname, username, password, errorMsg;
    bool success;
    QTcpSocket *socket;

public:

    ConnectThread(QTcpSocket *sock, QString host, QString user, QString pass){
        socket = sock;
        hostname = host;
        username = user;
        password = pass;
        success = false;
    }

    QTcpSocket *getSocket()
    { return socket; }

    QString GetErrorMSG(){
        return errorMsg;
    }

    bool GetSuccess(){
        return success;
    }

 void run();
};

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT
    
public:
    explicit MainWindow(QWidget *parent = 0);

    void addtoSelected(QLabel *label);
    ~MainWindow();
    int truckCounter;
    int soldierCounter;
    int time;
    QString style;

    QString Mensage;


    bool gameStarted;
    bool multyPlayerStarted;

    /*
    //FOR MULTIPLAYER

       bool infoMultiSent;
       bool infoMultiRecei;
       string mensageRecei;
       string mensageSent;
       int playerPos;
       bool playerPosAssigned;
       bool possCheapWork;

*/
    //SERVER THINGS
    QVector<QString> connectedPlayers;
    QVector<QString> joinedPlayer;
       int playerCount;
       int playerPosstion;
       int playerCount4Client;
       bool newPlayerConnected;
       bool GUIDone;

          //FOR GAME
/*
          bool toBuild;
          bool SecondPlConn;
          */
          string command;
          QString qCommand;
          QString jugador;
          QString hostName;

    //BUTTONS FOR THE GAME

          QPushButton *SinglePlayer;
          QPushButton *MultiPlayer;
          QPushButton *ExitGame;
          QPushButton *StartGame;
          QPushButton *CreateGame;
          QPushButton *JoinGame;

          QLabel *playerName;
          QTextEdit *currentPlayerName;
          QLabel *ComputerName;
          QLabel *currentComputerName;
          QLabel *CreatorHost;
          QTextEdit *CreatorName;
          QTextEdit *ChatConnection;
          QTextEdit *ChatTextPlace;
          QPushButton *SendChat;

          QLabel *player2;
          QLabel *player2Name;
          QLabel *player3;
          QLabel *player3Name;
          QLabel *player4;
          QLabel *player4Name;



    
private slots:
    void startGame();

    void exitGame();

    void timerHit();

    void on_btnBuildUnit_clicked();

    void on_btnBuildSoldier_clicked();

    void movementOfWindowUser();

    void on_btnBuildTank_clicked();

    void bldTank(int team, int Xpos, int Ypos);

    void SetPlayerName();

    void StartSinglePlayer();

    void StartMultiPlayer();

    void SetChatAbility();

    void JoinMultyGame();

    void CreateMultyGame();

    void SendMessages();

    void GetPlayerGUI(QString connectedPlayers);

    void SetGUIBeforeStart(QVector<QString> &vector);

    void StartMultyOfPlayer1(int totalPlayers);
    void StartMultyOfPlayer2(int totalPlayers);
    void StartMultyOfPlayer3(int totalPlayers);
    void StartMultyOfPlayer4(int totalPlayers);

    void ToHideTheServrCreator();

    void MoveThroughNetwork(int id, int x, int y);




    //-///////////////////////// SERVER THING ///////////////////

    void clientConnected();
    void dataReceived();
    void clientDisconnected();

    //-//////////////////////  CLIENT THING ////////////////

    void dataReceived_Client();

    void serverDisconnected();

    void connectFinished();

      //Multiplayer game start----------------

      void MultiCommand(string command);

      void MultiAction(QString comm);

      //MULTYPLAYER COMMAD

      void StartNetworkGame();

    //  void MovementOfWindow2User();

      void MultyBuildSoldier(int jugadorr, int x, int y);

      void MultyBuildCar(int jugadorr, int x, int y);




private:
    vector<MovableLabel*> objects;
    Ui::MainWindow *ui;

      QPoint p;

      int numX;
      int numY;

      int numX2;
      int numY2;

      MovableLabel *selected;
      QTimer *timer;

      //-------SERVER THING
      QTcpServer server;
      int connectCount;

      //----------------CLIENT THING

      //------TRY OUT
      QPushButton *button;

      ConnectThread *thread;
      QTcpSocket *socket;


};

#endif // MAINWINDOW_H
